package restless.menu {
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.text.TextFormat;
	import restless.core.gameObjs.RLEButton;
	import restless.core.gameObjs.RLEManagedObj;
	import restless.core.gameObjs.RLETextObj;
	import restless.core.interfaces.I2d;
	import restless.core.interfaces.IDispose;
	import restless.core.interfaces.IStage;
	import restless.core.managers.RLEKeyboardManager;
	import restless.core.managers.RLEMouseManager;
	import restless.core.managers.RLERender;
	import restless.core.gameObjs.RLEBlitMC;
	import restless.core.gameObjs.RLEMenuObj;
	import restless.core.gameObjs.RLEButton;
	import restless.core.interfaces.iMenu;
	
	/**
	 * 
	 * @author Jordan Laine
	 * 
	 */
	
	public class Menu extends RLEMenuObj implements iMenu {
		protected var bInit:Boolean = false;
		protected var bHorizontal:Boolean;
		protected var tempMenu:iMenu;
		protected var nButtons:int = 0;	
		protected var tempButton:RLEButton;
		protected var btnBack:RLEButton;
		private var bBKG:Boolean = false;
		public var headline:RLETextObj;
		protected var parentMenuButton:RLEButton;
		
		protected var selectedButton:int = -1;
		
		protected var defaultMenuActions:Vector.<String> = new < String > ["UP", "DOWN", "ENTER"];
		
		protected var vSubMenus:Vector.<iMenu>;
		protected var vButtons:Vector.<RLEButton>;
		protected var vButtonText:Vector.<String> = new Vector.<String>;
		protected var background:RLEBlitMC;
		
		public function Menu(parentMenu:iMenu = null) {
			setRE();
			vButtons = new Vector.<RLEButton>;
			vInputActions = defaultMenuActions;
			
			trace("creating " + sID + " menu");
			if (parentMenu != null) {
				super(parentMenu);
				createBackButton();
			}
			createParentMenuButton();
			
		}
		
		public function loadSubMenus(...args):void {
			i = args.length;
			vSubMenus = new Vector.<iMenu>(i);
			i--;
			for (i; i > -1; i--) {
				tempMenu = args[i] as iMenu;
				vSubMenus[i] = tempMenu;
				tempButton = tempMenu.getParentMenuButton();
				vButtons.push(tempButton);
				nButtons++;
			}
			createButtons();
		}
		
		public function createBackButton():void {
			btnBack = addButton();
		}
		
		public function back():void {
			//TODO Add functionality for menu back
		}
		
		protected function createParentMenuButton():void {
			parentMenuButton = new RLEButton(parentMenu);
		}
		
		public function getParentMenuButton():RLEButton {
			return parentMenuButton;
		}
		
		public function createMenu():void {
			
		}
		
		public function createButtons():void {
			
		}
		
		public function addButton():RLEButton {
			tempButton = new RLEButton(this);
			i = vButtons.push(tempButton);
			tempButton.nID = nButtons;
			nButtons++;
			return tempButton;
		}
		
		override public function set activate(bool:Boolean):void {
			if (bool == bActivate) { return; }
			bActivate = bool;
			updating = bActivate;
			keyboard = bActivate;
			visible = bActivate;
			
			if (headline != null) {
				headline.updating = (bActivate);
				headline.visible = bActivate;
			}
			
			if (background != null) { 
				background.updating = (bActivate);
				background.visible = bActivate;
			}
			i = vButtons.length - 1;
			for (i; i > -1; i--) {
				vButtons[i].activate = bActivate;
			}
		}
		
		public function click(num:int):void {
			activate = false;
			
			i = nButtons-1;
			for (i; i > -1; i--) {
				if (vSubMenus[i].nID == num) {
					vSubMenus[i].activate = true;
				}
			}
		}
		
		override public function update():void {
			if (parentMenu) {
				parentMenuButton.x 
			}
		}
		
		protected function init():void {
			bInit = true;
		}
		
		override public function input(action:String):void {
			tempString = action;
			
			if (selectedButton < 0) {
				selectedButton = 0;
				updateButtons();
				return;
			} else if (selectedButton > nButtons-1) {
				selectedButton = 0;
				updateButtons();
				return;
			}
			
			switch (tempString) {
				case "UP":
					if (selectedButton > 0) {
						selectedButton--;
					}
					break;
					
				case "DOWN":
					if (selectedButton > -1 && selectedButton < nButtons-1) {
						if (selectedButton < nButtons) {
							selectedButton++;
						}
					}
					break;
					
				case "ENTER":
					if ( selectedButton > -1 && selectedButton <= nButtons ) { click(selectedButton); }
					break;
			}
			
			updateButtons();
		}
		
		public function mouseOver(id:int):void {
			selectedButton = id;
		}
		
		public function updateButtons():void {
			deselectButtons();
			vButtons[selectedButton].over();
		}
		
		public function deselectButtons():void {
			i = nButtons-1;
			for (i; i > -1; i--) {
				vButtons[i].out();
			}
		}
		
		public function setBkg(obj:RLEBlitMC):void {
			bBKG = true;
			background = obj;
			background.x = x;
			background.y = y;
		}
		
		public function removeBkg():void {
			if (bBKG) {
				background.dispose();
				background = null;
			}
		}
		
		public function set selectedBtn(num:int):void {
			selectedButton = num;
		}
		
		public function get selectedBtn():int {
			return selectedButton;
		}
		
		public function disposeButtons():void {
			i = vButtons.length-1;
			for (i; i > -1; i--) {
				vButtons[i].dispose();
			}
			vButtons.length = 0;
		}
		
		override public function dispose():void {
			activate = false;
			disposeButtons();
			removeBkg();
			vButtons = null;
			vInput = null;
			tempButton = null;
			
			background = null;
			btnBack = null;
			tempButton = null;
			parentMenuButton = null;
			tempMenu = null;
			
			super.dispose();
		}
	}
}